package com.wn.airplane_mode.tool;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.provider.Settings;
import android.widget.TabHost;

import com.wn.airplane_mode.activity.AirplaneModeOpNewActivity;
import com.wn.airplane_mode.service.AirplaneModeOpService;
/**
 * @author wuning
 * */
public class AirplaneTool {
	
	private static final String PREFERENCESNAME = "AirplaneModeOpPref";

	
	private static final String AUTO_STARTUP_SERVICE_KEY = "auto_startup_service";
	private static final String ICON_ON_STATUSBAR_KEY = "icon_on_statusbar";
	//private static final String NOW_STARTUP_AIRPLANEMODE_KEY = "now_startup_airplanemode";
	private static final String SERVICE_STATUS_KEY = "service_status";
	
	private static final String TASKOBJ_STRING_VALUE_KEY = "taskobj_string_value";
	
	/**时分分隔符*/
	public static final String SPLIT_H_M = ":";
	public static final String DAY_OF_WEEK_KEY = "day_of_week";
	public static final String TASKOBJ_KEY = "task_obj";
	

	private static SharedPreferences preferences = null;
	private static Editor editor = null;
	private static AirplaneTool airplaneTool = null;
	private static Context context = null;
	
	private AirplaneModeOpNewActivity.AirplaneModeHandler handler = null;
	
	private AirplaneTool(Context ct){
		
		context = ct;
		preferences = context.getSharedPreferences(PREFERENCESNAME, ContextWrapper.MODE_PRIVATE);
		editor = preferences.edit();
	}
	
	public static AirplaneTool getInstance(Context context){
		if(context == null){
			throw new NullPointerException("ContextWrapper is null.");
		}
		if(airplaneTool == null){
			synchronized (AirplaneTool.class) {
				if(airplaneTool == null){
					airplaneTool = new AirplaneTool(context);
				}
			}
		}
		return airplaneTool;
	}
	
//	/**设是否启动飞行模式*/
//	public boolean setNowAirplanemode(boolean is){
//		
//		return editor.putBoolean(NOW_STARTUP_AIRPLANEMODE_KEY, is).commit();
//	}
//	/**是否启动飞行模式*/
//	public boolean isNowAirplanemode(){
//		return preferences.getBoolean(NOW_STARTUP_AIRPLANEMODE_KEY, false);
//	}
	
	/**设置图标是否常驻状态栏*/
	public boolean setIconOnstatusbar(boolean is){
		return editor.putBoolean(ICON_ON_STATUSBAR_KEY, is).commit();
	}
	/**图标是否常驻状态栏*/
	public boolean isIconOnstatusbar(){
		return preferences.getBoolean(ICON_ON_STATUSBAR_KEY, false);
	}
	
	/**设置是否自动开启后台服务*/
	public boolean setAutoStartupService(boolean is){
		return editor.putBoolean(AUTO_STARTUP_SERVICE_KEY, is).commit();
	}
	/**开机是否自动开启后台服务*/
	public boolean isAutoStartupService(){
		return preferences.getBoolean(AUTO_STARTUP_SERVICE_KEY, false);
	}
	
	public boolean setTaskObjStr(AirplaneTaskObj obj){
		return editor.putString(TASKOBJ_STRING_VALUE_KEY+obj.getDayId(), obj.toString()).commit();
	}
	
	public String getTaskObjStr(int dayId){
		return preferences.getString(TASKOBJ_STRING_VALUE_KEY+dayId, dayId+AirplaneTaskObj.SPLIT_Field+"07:00"+AirplaneTaskObj.SPLIT_Field+"23:00"+AirplaneTaskObj.SPLIT_Field+"false"+AirplaneTaskObj.SPLIT_Field+"false");
	}

	public boolean isAirPlaneModeOn(){
    	//1-表示飞行模式
    	return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,0) == 1? true:false;
    	
    }
	
	/**
     * @param enable true - turn on mode, false - turn off mode
     * @return true if set value successful,or false if set value unsuccessful
     * */
    public boolean setAirPlaneModeOn(boolean enabled){
    	boolean value = Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, enabled ?1:0);
    	//通知其他的网络应用设备，飞行模式状态已经改变
    	if(value){
    		Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);   
    		intent.putExtra("state", enabled);//绑定state 值可以解决部分机型不能正常自动打开飞行模式的问题
    		context.sendBroadcast(intent);   

    		//context.sendBroadcast(new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED));
    	}
    	
    	return value;
    }
    
//    public boolean setServiceRunning(boolean isRunning){
//		return editor.putBoolean(SERVICE_STATUS_KEY, isRunning).commit();
//	}
    
    /**后台服务是否正在运行*/
	public boolean isServiceRunning(){
		//Log.i("AirplaneTool", "check isServiceRunning");
		ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> services = am.getRunningServices(Integer.MAX_VALUE);
		for (RunningServiceInfo runningServiceInfo : services) {
			//Log.i("AirplaneTool", runningServiceInfo.service.getClassName());
			if(runningServiceInfo.service.getClassName().equals(AirplaneModeOpService.class.getName())){
				//Log.i("AirplaneTool", "isServiceRunning true");
				return true;
			}
		}
		return false;
		//return preferences.getBoolean(SERVICE_STATUS_KEY, false);
	}
    
	public void setAirplaneModeHandler(AirplaneModeOpNewActivity.AirplaneModeHandler handler){
		this.handler = handler;
	}
	public AirplaneModeOpNewActivity.AirplaneModeHandler getAirplaneModeHandler(){
		return handler;
	}
	
	StringBuffer sb = new StringBuffer();
	public String genTimeString(int hourOfDay, int minute){
		sb.delete(0, 6);
		if(hourOfDay<10){
			sb.append("0");
		}
		sb.append(hourOfDay);
		sb.append(":");
		if(minute<10){
			sb.append("0");
		}
		sb.append(minute);
		return sb.toString();
	}
	
	public TabHost tabHost;
	
	public void reset(){
		airplaneTool = null;
	}
}
